package com.atguigu.flink.day03;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.func.MyMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/8/12
 * 该案例演示了转换算子-map
 * 需求：提取WaterSensor中的id字段
 */
public class Flink09_Transform_Map {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //TODO 2.准备数据
        DataStreamSource<WaterSensor> stream = env.fromElements(
                new WaterSensor("sensor_1", 1L, 1),
                new WaterSensor("sensor_2", 2L, 2)
        );
        //TODO 3.转换
        //匿名内部类方式创建对象
        /*SingleOutputStreamOperator<String> mapDS = stream.map(
                new MapFunction<WaterSensor, String>() {
                    @Override
                    public String map(WaterSensor ws) throws Exception {
                        return ws.id;
                    }
                }
        );

        //lambda方式创建对象
        SingleOutputStreamOperator<String> mapDS = stream.map(
                ws -> ws.id
        );


        //方法引用方式创建对象
        SingleOutputStreamOperator<String> mapDS = stream.map(WaterSensor::getId);
         */

        //单独抽取类实现接口
        SingleOutputStreamOperator<String> mapDS = stream.map(
                new MyMapFunction()
        );

        //TODO 4.打印
        mapDS.print();

        //TODO 5.提交作业
        env.execute();


    }
}
